package com.taxx.controller;

import com.taxx.service.ExcelImportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * Excel导入控制器
 * 
 * @author taxx
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequestMapping("/excel")
@CrossOrigin
public class ExcelImportController {

    @Autowired
    private ExcelImportService excelImportService;

    /**
     * 导入收入信息Excel
     */
    @PostMapping("/import/income")
    public ResponseEntity<Map<String, Object>> importIncomeExcel(
            @RequestParam("file") MultipartFile file,
            @RequestParam("userId") Long userId,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        
        log.info("开始导入收入信息Excel，文件：{}，用户ID：{}，起止时间：{} - {}",
                file.getOriginalFilename(), userId, startTime, endTime);
        
        try {
            Map<String, Object> result = excelImportService.importIncomeExcel(file, userId, startTime, endTime);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("导入收入信息Excel失败", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("success", false);
            errorResult.put("message", "导入失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(errorResult);
        }
    }

    /**
     * 导入身份信息Excel
     */
    @PostMapping("/import/identity")
    public ResponseEntity<Map<String, Object>> importIdentityExcel(
            @RequestParam("file") MultipartFile file,
            @RequestParam("userId") Long userId) {
        
        log.info("开始导入身份信息Excel，文件：{}，用户ID：{}",
                file.getOriginalFilename(), userId);
        
        try {
            Map<String, Object> result = excelImportService.importIdentityExcel(file, userId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("导入身份信息Excel失败", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("success", false);
            errorResult.put("message", "导入失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(errorResult);
        }
    }

    /**
     * 导入平台信息Excel
     */
    @PostMapping("/import/platform")
    public ResponseEntity<Map<String, Object>> importPlatformExcel(
            @RequestParam("file") MultipartFile file,
            @RequestParam("userId") Long userId,
            @RequestParam(value = "reportDate", required = false) String reportDate) {
        
        log.info("开始导入平台信息Excel，文件：{}，用户ID：{}，上报日期：{}", 
                file.getOriginalFilename(), userId, reportDate);
        
        try {
            Map<String, Object> result = excelImportService.importPlatformExcel(file, userId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("导入平台信息Excel失败", e);
            Map<String, Object> errorResult = new HashMap<>();
            errorResult.put("success", false);
            errorResult.put("message", "导入失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(errorResult);
        }
    }
}
